/*
 * @Author: your name
 * @Date: 2021-11-09 16:30:21
 * @LastEditTime: 2021-11-18 08:37:55
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\controller\home.js
 */
"use strict";

const Controller = require("../core/base_controller");
class UserController extends Controller {
    //登录
    async login() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.login();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //注册
    async register() {
        const { ctx, app } = this;
        try {
            const res = await ctx.service.user.register();
            this.success(res);
        } catch (err) {
            this.error(err.message);
        }
    }

    //刷新token
    async refresh() {
            // 刷新token
            // 获取旧token 返回新token
            const { ctx, app } = this;
            const { authorization } = ctx.request.header;
            const { token } = authorization.split(" ")[1];
            const jwt = require("jsonwebtoken");
            const user = jwt.verify(token, app.config.keys);
            const newToken = jwt.sign({ id: user.id, nickname: user.nickname },
                app.config.keys, { expiresIn: "2h" }
            );
            try {
                this.success({
                    token: newToken,
                    expiresIn: Date.now() + 2 * 60 * 60 * 1000,
                });
            } catch (err) {
                this.error(err.message);
            }
        }
        //修改密码
    async changePassword() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.changePassword();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //获取用户信息(单个)
    async getUserInfo() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.getUserInfo();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //获取用户信息(全部)
    async getAllUser() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.getAllUser();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //修改用户信息
    async editUserInfo() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.editUserInfo();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //改变用户可用状态
    async changeUserState() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.changeUserState();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //改变用户身份
    async changeUseIdentity() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.changeUseIdentity();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //批量启用
    async batchUserStart() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.batchUserStart();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //批量禁用
    async batchUserForbid() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.batchUserForbid();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //批量授权
    async batchUserAllow() {
            const { ctx, app } = this;
            try {
                const res = await ctx.service.user.batchUserAllow();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //批量解除授权
    async batchUserRelieve() {
        const { ctx, app } = this;
        try {
            const res = await ctx.service.user.batchUserRelieve();
            this.success(res);
        } catch (err) {
            this.error(err.message);
        }
    }
}

module.exports = UserController;